#!/usr/bin/env python
#-*-coding:utf-8-*-
#计算聚类结果中,每行的平均使用频次
print "输入格式 python week_avg_uv.py 输入文件  输出结果"
import sys
import os
#第一个参数,聚类的中间文件,第二个参数,聚类的结果文件,第三个参数,一共有多少维度
if len(sys.argv) != 4:
    print "argv's length is not 3"
    sys.exit()
analysis_file_path = sys.argv[1]
analysis_result_file_path = sys.argv[2]
column_length = sys.argv[3]
if not os.path.exists(analysis_file_path):
    print 'file: %s does not exist' % analysis_file_path
    sys.exit()
elif os.path.exists(analysis_result_file_path):
    print 'file: %s exist' % analysis_result_file_path
    sys.exit()
elif not column_length.isdigit():
    print 'arg: %s is not a digit' % column_length
    sys.exit()
else:
    column_length_value = int(column_length)
    analysis_file = open(analysis_file_path)
    analysis_result_file = open(analysis_result_file_path,"w+")
    for line in analysis_file:
        columns = line.rstrip().lstrip().split(" ")
        column_name = columns[0]
        colunm_avg_value = 0.0
        for sub_column in columns[1:]:
            week_number_week_value = sub_column.split(":")
            week_number_value = int(week_number_week_value[0])
            if week_number_value <= column_length_value:
                week_value = float(week_number_week_value[1])
                colunm_avg_value += week_value
            else:
                break
        print >>analysis_result_file,column_name,colunm_avg_value/column_length_value            
    analysis_file.close()
    analysis_result_file.close()
    print "run over"
    